using System;
using System.Collections.Generic;
using System.Text;

namespace OfficeClip.OpenSource.Utility
{
 public class SqlConnectionString
 {
	private bool _isAuthenticate;
	private string _password;
	private string _userId;
	private string _database;
	private string _server;

	public string Server
	{
	 get
	 {
		return _server;
	 }
	 set
	 {
		_server = value;
	 }
	}

	public string Database
	{
	 get
	 {
		return _database;
	 }
	 set
	 {
		_database = value;
	 }
	}

	public string UserId
	{
	 get
	 {
		return _userId;
	 }
	 set
	 {
		_userId = value;
	 }
	}

	public string Password
	{
	 get
	 {
		return _password;
	 }
	 set
	 {
		_password = value;
	 }
	}

	public bool IsAuthenticate
	{
	 get
	 {
		return _isAuthenticate;
	 }
	 set
	 {
		_isAuthenticate = value;
	 }
	}

	public SqlConnectionString(string cString)
	{
	 string[] astr = cString.Split(";".ToCharArray());
	 string key, val;
	 foreach (string str in astr)
	 {
		key = str.Substring(0, str.IndexOf("="));
		val = str.Substring(str.IndexOf("=") + 1);
		switch (key.ToUpper())
		{
		 case "SERVER":
			_server = val;
			break;
		 case "DATABASE":
			_database = val;
			break;
		 case "USER ID":
		 case "UID":
			_userId = val;
			break;
		 case "PASSWORD":
		 case "PWD":
			_password = val;
			break;
		 case "TRUSTED_CONNECTION":
			_isAuthenticate = ((val.ToUpper() == "TRUE") || (val.ToUpper() == "YES"));
			break;
		}
	 }
	}

	public SqlConnectionString(SqlConnectionString oldConn)
	{
	 _server = oldConn.Server;
	 _database = oldConn.Database;
	 _userId = oldConn.UserId;
	 _password = oldConn.Password;
	 _isAuthenticate = oldConn.IsAuthenticate;
	}

	public SqlConnectionString(string server, string database, string userId, string password, bool isAuthenticate)
	{
	 _server = server;
	 _database = database;
	 _userId = userId;
	 _password = password;
	 _isAuthenticate = isAuthenticate;
	}

	public SqlConnectionString(string server, string database)
	{
	 _server = server;
	 _database = database;
	 _userId = string.Empty;
	 _password = string.Empty;
	 _isAuthenticate = true;
	}

	public new string ToString()
	{
	 return ToString(false);
	}

	public string ToString(bool isHideCredential, bool isMaster)
	{
	 string dbName = (isMaster) ? "master" : _database;
	 return (_isAuthenticate) ?
		string.Format(
			"Server={0};Database={1};Trusted_Connection=True",
			_server, dbName
			) :
		string.Format(
		 "Server={0};Database={1};User ID={2};Password={3};Trusted_Connection=False",
		 _server, dbName, (isHideCredential) ? "***" : _userId,
		 (isHideCredential) ? "***" : _password
	 );
	}

	public string ToString(bool isHideCredential)
	{
	 return ToString(isHideCredential, false);
	}

	public bool IsEqual(SqlConnectionString scs)
	{
	 return (
		(scs._isAuthenticate == _isAuthenticate) &&
		(scs._password == _password) &&
		(scs._server == _server) &&
		(scs._userId == _userId) &&
		(scs._database == _database)
		);
	}
 }
}
